//
//  ClearCache.m
//  Tools
//
//  Created by Jasmine on 16/6/16.
//  Copyright © 2016年 Jasmine. All rights reserved.
//

#import "ClearCache.h"
#import "UIImageView+WebCache.h"

@implementation ClearCache

#pragma mark - 计算单个文件大小
+ (float)fileSizeOfPath:(NSString *)path
{
    // 创建管理器对象
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        // 计算大小
        long long size = [manager attributesOfItemAtPath:path error:nil].fileSize;
        return size/1024.0/1024.0;
    }
    return 0;
}
#pragma mark - 计算目录大小
+ (float)folderOfSize:(NSString *)path
{
    // 创建管理器对象
    
    NSFileManager *manager = [NSFileManager defaultManager];
    float folderSize;
    if ([manager fileExistsAtPath:path]) {
        // 遍历所有文件, 就算大小
        NSArray *arrOfChilderFile = [manager subpathsAtPath:path];
        for (NSString *fileName in arrOfChilderFile) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            folderSize += [[self class] fileSizeOfPath:absolutePath];
            
        }
        folderSize += [[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;
        return folderSize;
    }
    return 0;
}
#pragma mark - 清除缓存
+ (void)clearCache:(NSString *)path{
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        NSArray *childerFiles = [manager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            [manager removeItemAtPath:absolutePath error:nil];
            
        }
        [[SDImageCache sharedImageCache]cleanDisk];
    }
    
}

@end
